/**
 * @className AnnouncementController
 * @author liuxuelin
 * @Date 2021/1/18
 * @version v1.0
 */

package com.hidt.shop.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hidt.shop.domain.Announcement;
import com.hidt.shop.service.AnnouncementService;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;
    /**
     * 添加公告的方法
     *@参数  announcement
     *@返回值  int
     *@创建人  liuxuelin
     *@创建时间  2021/1/18
     */
    @RequestMapping(value = "/announcement/add")
    @ResponseBody
    public String addAnnouncement(@RequestBody Announcement announcement){
        System.out.println(announcement.getNotice_title());
        int i = announcementService.addAnnouncement(announcement);
        if (i>0) {
            return "添加成功1";
        }else{
            return "添加失败0";
        }
    }
//    public String addAnnouncement(HttpServletRequest request) throws JsonProcessingException {
//        System.out.println("111111");
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        Set<String> stringSet = parameterMap.keySet();
//        Announcement announcement = null;
//        for (String  key:stringSet){
//            System.out.println(key);
//            ObjectMapper mapper=new ObjectMapper();
//            announcement= mapper.readValue(key, Announcement.class);
//            break;
//        }
//        System.out.println(announcement.getNotice_title());
//        int i = announcementService.addAnnouncement(announcement);
//        if (i>0) {
//            /*m.addAttribute("message","添加成功");*/
//            return "添加成功1";
//        }else{
//            /*m.addAttribute("message","添加失败");*/
//            return "添加失败0";
//        }
//    }
    /**
     * 查找公告的方法
     *@参数
     *@返回值
     *@创建人  liuxuelin
     *@创建时间  2021/1/18
     */
    @RequestMapping("/announcement/findall")
    @ResponseBody
    public List<Announcement> findAllAnnouncement(){
        List<Announcement> all = announcementService.findAll();
//        String ann= JSON.toJSONString(all);
//        System.out.println(all);
        return all;
    }

    /**
     * 查找公告的方法
     *@参数
     *@返回值
     *@创建人  liuxuelin
     *@创建时间  2021/1/18
     */
    @RequestMapping("/announcement/findall2")
    @ResponseBody
    public List<Announcement> findAllAnnouncement2(@RequestBody Map<String,String> map){
        int noticeis_delete = Integer.parseInt(map.get("noticeis_delete"));
        List<Announcement> all = announcementService.findAll2(noticeis_delete);
//        String ann= JSON.toJSONString(all);
//        System.out.println(all);
        return all;
    }
    /**
     * 删除公告的方法（改变公告表的状态）
     *@参数
     *@返回值
     *@创建人  liuxuelin
     *@创建时间  2021/1/18
     */
    @RequestMapping("/announcement/del")
    @ResponseBody
    public String updateAnnouncement(@RequestBody Announcement announcement){
        int i = announcementService.updateAnnouncement(announcement);
        System.out.println(announcement.getNotice_id());
        System.out.println(announcement.getNoticeis_delete());
        if (i>0) {
            /*m.addAttribute("message","添加成功");*/
            return "删除成功";
        }else{
            /*m.addAttribute("message","添加失败");*/
            return "删除失败";
        }
    }
    /**
     * 修改已经发布的公告
     *@参数
     *@返回值
     *@创建人  liuxuelin
     *@创建时间  2021/1/18
     */
    @RequestMapping("/announcement/update")
    @ResponseBody
    public String updateAnnouncement2(@RequestBody Announcement announcement){
        System.out.println(announcement.getNotice_id());
        System.out.println(announcement.getNotice_title());
        System.out.println(announcement.getNotice_context());
        int i = announcementService.updateAnnouncement2(announcement);

        if (i>0) {
            /*m.addAttribute("message","添加成功");*/
            return "修改成功";
        }else{
            /*m.addAttribute("message","添加失败");*/
            return "修改失败";
        }
    }
}
